<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\SkuReference;
use Carbon\Traits\Date;
use Illuminate\Http\Request;


class OrderTemplateController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Order Template';


    public function index(Request $req)
    {
        $all = $req->all();
//        $per_page = isset($all['per_page']) ? $all['per_page'] :20;
        $per_page = $this->getPerPage($req);

        $qry = SkuReference::query()
//            ->select(['created_at','sku'])
            ->orderBy('created_at','desc');

        if (isset($all['sku'])) {
            $qry->where('sku', 'like', '%'.$all['sku'].'%');
        }
        if (isset($all['reference'])) {
            $qry->where('reference', 'like',"%{$all['reference']}%");
        }
        if (isset($all['main_market'])) {
            $qry->where('main_market', 'like',"%{$all['main_market']}%");
        }

        // Date
        if (isset($all['date_start'])) {
            $qry->where('date','>=', $all['date_start']);
        }
        if (isset($all['date_end'])) {
            $qry->where('date','<=', $all['date_end']);
        }
        // Create time
        if (isset($all['created_start'])) {
            $qry->where('created_at','>=', $all['created_start'].' 00:00:00');
        }
        if (isset($all['created_end'])) {
            $qry->where('created_at','<=', $all['created_end'].' 23:59:59');
        }

        $list = $qry->paginate($per_page);
        foreach($list->items() as $item) {
            $item->_level = @SkuReference::levelOptions()[$item->level];
        }

        return $this->resSuccess('', $list);
    }


    protected function grid()
    {
        $grid = new Grid(new SkuReference);

        $grid->model()->orderBy('created_at','desc');
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1/4, function($filter) {

                $filter->like('sku', 'SKU');
//                $filter->where(function($q) {
//                    \Log::info('you');
//                    $q->where('sku','like', "%{$this->input}%");
//
//                },'SKU');

            });

            $filter->column(1/4, function($filter) {
                $filter->where(function($q) {
                    $q->where('reference', 'like',"%{$this->input}%");
                },'Reference');
            });
            $filter->column(1/4, function($filter) {
                $filter->like('main_market', 'Main_market');
            });
            $filter->column(1/4, function($filter) {
                $filter->between('date', 'Date')->date();
            });
            $filter->column(1/4, function($filter) {
                $filter->between('created_at', 'Create Time')->date();
            });


        });

        $grid->column('date')->width(100);
        $grid->column('sku', 'SKU');
        $grid->column('level')->using(
            SkuReference::levelOptions()
        );
        $grid->column('reference');
        $grid->column('description');
        $grid->column('main_market', 'Main Market');
        $grid->column('de_weekly_qty', 'DE -Weekly Qty');
        $grid->column('us_weekly_qty', 'US-Weekly Qty');
        $grid->column('uk_weekly_qty', 'UK-Weekly Qty');
        $grid->column('au_weekly_qty', 'AU-Weekly Qty');
        $grid->column('note');
        $grid->column('us_fba', 'US FBA');
        $grid->column('de_fba', 'DE FBA');
        $grid->column('remark','备注');
        $grid->column('user.name','Creator');
        $grid->column('created_at','Create Time');

        $grid->column('combination','组合内容')->hide();
        $grid->column('combination_num','组合对应个数')->hide();
        $grid->column('customs_name_cn','中文报关品名')->hide();
        $grid->column('customs_name_en','英文报关品名')->hide();
        $grid->column('out_unit','出口单位')->hide();
        $grid->column('brand','有无品牌')->hide();
        $grid->column('out_unit_qty','出口单位数')->hide();
        $grid->column('car_type','车型')->hide();

//        $grid->disableCreateButton();
        $this->disableButton(['create']);

        $grid->disableBatchActions();

        $grid->disableActions();

        $this->toolsButton(new ImportOrderTemplateAction()); //导入
        $this->toolsButton(new GotoButtonAction('异常','/admin/orderTemplateExp', 'btn-warning'));


        //
        $grid->export(function($export) {
            $export->except(['description']);
            $export->column('part_name_en', function ($value, $original) {
                 return $value;
            });
        });




        return $grid;
    }



    public function form()
    {

        $form = new Form(new SkuReference());
        $form->disableViewCheck();
        $form->disableCreatingCheck();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });


        $form->column(1/2, function($form) {
            $form->display('sku')->required();
            $form->display('reference')->required();
            $form->date('date');
            $form->text('part_name_en');
            $form->text('part_name_cn');
            $form->select('level')->options(
                SkuReference::levelOptions()
            )->required();
            $form->text('description');
            $form->text('qty')->rules('numeric')->required();
            $form->text('cost')->rules('numeric');
            $form->text('total_cost')->rules('numeric');
            $form->text('cost_note');
            $form->text('exchange_rate')->rules('numeric');
            $form->text('suggested_price')->rules('numeric');
            $form->text('profit_pct', 'Profit(%)')->rules('numeric');
            $form->text('profit')->rules('numeric');
            $form->text('combination','组合内容');
            $form->text('combination_num','组合对应个数');
            $form->text('customs_name_cn','中文报关品名');
            $form->text('out_unit','出口单位');
            $form->text('brand','有无品牌');
        });

        $form->column(1/2, function($form) {
            $form->text('annual_return');
            $form->text('main_market','Main Market');
            $form->text('de_weekly_qty','DE -Weekly Qty')->rules('numeric');
            $form->text('de_feference_link','DE-Reference Link');
            $form->text('us_weekly_qty','US -Weekly Qty')->rules('numeric');
            $form->text('us_feference_link','US-Reference Link');
            $form->text('uk_weekly_qty', 'UK -Weekly Qty')->rules('numeric');
            $form->text('uk_feference_link','UK-Reference Link');
            $form->text('au_weekly_qty', 'AU -Weekly Qty')->rules('numeric');
            $form->text('au_feference_link','AU-Reference Link');
            $form->text('note');
            $form->text('us_fba', 'US FBA')->rules('numeric');
            $form->text('de_fba', 'DE FBA')->rules('numeric');
            $form->text('factory_no','工厂号');
            $form->text('tq','TQ');
            $form->text('remark','备注');
            $form->text('customs_name_en','英文报关品名');
            $form->text('out_unit_qty','出口单位数');
            $form->text('car_type','车型');


        });

        return $form;
    }




}
